package com.proton.carepatchtemp.base;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.apkfuns.logutils.LogUtils;
import com.baidu.mobstat.StatService;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.constant.Settings;
import com.proton.carepatchtemp.utils.eventbus.MainBusListener;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.ui.FlymeStatusbarUtils;
import com.proton.carepatchtemp.utils.ui.MIUIStatusBarUtils;
import com.proton.carepatchtemp.utils.ui.StatusBarUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public abstract class BaseActivity extends AppCompatActivity implements MainBusListener {

    private static final String TAG = "BaseActivity";
    private static final int REQUEST_CODE_REQUEST_PERMISSION = 1;

    public abstract void dealEvent(MainEvent event);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置状态栏文字颜色为深色  针对flyme miui 和android 6.0 以上
        if (isDarkIcon()) {
            if (FlymeStatusbarUtils.isMeizuFlymeOS()) {
                FlymeStatusbarUtils.setStatusBarDarkIcon(BaseActivity.this, true);
            } else if (MIUIStatusBarUtils.isMIUI()) {
                //miui状态栏适配
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                } else {
                    MIUIStatusBarUtils.setStatusBarDarkMode(true, BaseActivity.this);
                }
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        }

        PushAgent.getInstance(this).onAppStart();
        MobclickAgent.setScenarioType(BaseActivity.this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        registerEventBus();
    }

    protected boolean isDarkIcon() {
        return true;
    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        setStatusBar();
    }

    protected void setStatusBar() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(VCare.get(), R.color.colorPrimaryDark));
    }

    /**
     * 点击事件初始化
     */
    protected abstract void setListener();

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getName());
        MobclickAgent.onResume(this);
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getName());
        MobclickAgent.onPause(this);
        StatService.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegisterEventBus();
    }

    //不随系统字体缩放改变
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        config.fontScale = 1;
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    protected void jumpToActivty(Class activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }

    private void registerEventBus() {
        EventBus.getDefault().register(BaseActivity.this);
    }

    public void unRegisterEventBus() {
        EventBus.getDefault().unregister(BaseActivity.this);
    }

    public boolean canDeal(MainEvent event) {
        return event != null && !isFinishing();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void dealOnEvent(MainEvent event) {
        if (canDeal(event)) {
            try {
                dealEvent(event);
            } catch (Throwable e) {
                LogUtils.e(e);
            }
        }
    }

    //获取动态权限
    public void getPermissins() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return;
        } else {
            for (String strPermission : Settings.Permissions) {
                if (ContextCompat.checkSelfPermission(this, strPermission) == PackageManager.PERMISSION_GRANTED) {
                    return;
                } else {
                    ActivityCompat.requestPermissions(this, new String[]{strPermission}, REQUEST_CODE_REQUEST_PERMISSION);
                }
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE_REQUEST_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.e(TAG, "获取" + permissions.toString() + "成功");
            } else {
                Log.e(TAG, "获取" + permissions.toString() + "失败");
            }
        }

    }

    protected String getName() {
        return getClass().getSimpleName();
    }
}
